package org.eclipse.football;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OffStatsServlet extends HttpServlet implements Servlet {
	// Override doPost function
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Read in information submitted by form
		String pass_yds_raw = request.getParameter("pass_yds");
		String pass_tds_raw = request.getParameter("pass_tds");
		String picks_raw = request.getParameter("picks");
		String rush_yds_raw = request.getParameter("rush_yds");
		String rush_tds_raw = request.getParameter("rush_tds");
		String rec_yds_raw = request.getParameter("rec_yds");
		String rec_tds_raw = request.getParameter("rec_tds");
		String fumbles_raw = request.getParameter("fumbles");
		String ret_tds_raw = request.getParameter("ret_tds");
		
		// Convert all input values to integers
		int pass_yds = stringToInt(pass_yds_raw);
		int pass_tds = stringToInt(pass_tds_raw);
		int picks = stringToInt(picks_raw);
		int rush_yds = stringToInt(rush_yds_raw);
		int rush_tds = stringToInt(rush_tds_raw);
		int rec_yds = stringToInt(rec_yds_raw);
		int rec_tds = stringToInt(rec_tds_raw);
		int fumbles = stringToInt(fumbles_raw);
		int ret_tds = stringToInt(ret_tds_raw);
		
		// Obtain existing league
		League league = (League) request.getSession(true).getAttribute("league");
		
		// Obtain existing team
		Team team = (Team) request.getSession(true).getAttribute("team");
		
		// Obtain existing offensive player
		OffPlayer player = (OffPlayer) request.getSession(true).getAttribute("off");
		
		// Add new stats to players old stats and set them in player
		player.setPass_yds(pass_yds+player.getPass_yds());
		player.setPass_tds(pass_tds+player.getPass_tds());
		player.setPicks(picks+player.getPicks());
		player.setRush_yds(rush_yds+player.getRush_yds());
		player.setRush_tds(rush_tds+player.getRush_tds());
		player.setRec_yds(rec_yds+player.getRec_yds());
		player.setRec_tds(rec_tds+player.getRec_tds());
		player.setFumbles(fumbles+player.getFumbles());
		player.setRet_tds(ret_tds+player.getRet_tds());

		// Set player back to null
		player = null;
		
		// Write the state of the league out to a file
		league.writeState("league.txt");
		
		// Set league in the session
		request.getSession().setAttribute("league", league);
		
		// Set team in the session
		request.getSession().setAttribute("team", team);
		
		// Set player in the session
		request.getSession().setAttribute("off", player);
		
		// Set def and kicker as null in the session
		request.getSession().setAttribute("def", null);
		request.getSession().setAttribute("kicker", null);
		
		// Forward control back to stats.jsp
		getServletContext().getRequestDispatcher("/TeamPage.jsp").forward(request, response);
		
		// TODO Auto-generated method stub
		//super.doPost(request, response);
	}

	// Define a wrapper for Integer.parseInt function
	int stringToInt(String in){
		// Check if input string is blank or null
		if(in == null || in.equalsIgnoreCase("")){
			// If so return 0
			return 0;
		}
		
		// Else convert string to int
		else{
			return Integer.parseInt(in);
		}
	}
}

